﻿using ErrorOr;
using Gym.Application.Common.Interfaces;
using MediatR;

namespace Gym.Application.Gyms.Queries.ListGyms
{
    public class ListGymsQueryHandler : IRequestHandler<ListGymsQuery, ErrorOr<List<Gym.Domain.GymAggregate.Gym>>>
    {
        private readonly IGymsRepository _gymsRepository;
        private readonly ISubscriptionsRepository _subscriptionsRepository;

        public ListGymsQueryHandler(IGymsRepository gymsRepository, ISubscriptionsRepository subscriptionsRepository)
        {
            _gymsRepository = gymsRepository;
            _subscriptionsRepository = subscriptionsRepository;
        }

        public async Task<ErrorOr<List<Domain.GymAggregate.Gym>>> Handle(ListGymsQuery request, CancellationToken cancellationToken)
        {
            if (!await _subscriptionsRepository.ExistsAsync(request.SubscriptionId))
            {
                return Error.NotFound(description:"没有找到订阅信息");
            }

            return await _gymsRepository.ListSubscriptionGymsAsync(request.SubscriptionId);
        }
    }
}
